# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=subversion
pkgver=1.14.0
pkgrel=3
pkgdesc="A Modern Concurrent Version Control System"
arch=('i686' 'x86_64')
url="https://subversion.apache.org/"
license=('APACHE')
groups=('VCS')
depends=('libsqlite' 'file' 'liblz4' 'libserf' 'libsasl')
makedepends=('python' 'python-py3c' 'perl' 'swig' 'ruby' 'liblz4-devel' 'libsqlite-devel' 'libserf-devel' 'libsasl-devel' 'gmp-devel')
optdepends=('bash-completion: for svn bash completion'
            'python: for some hook scripts'
            'ruby: for some hook scripts')
provides=('svn')
options=('!makeflags' '!libtool' '!emptydirs')
noextract=("subversion-${pkgver}.tar.bz2")
source=(https://archive.apache.org/dist/subversion/subversion-${pkgver}.tar.bz2{,.asc}
        svnclean
        01-make-build-directories.patch
        02-fix-linking.patch
        03-fix-linking-perl.patch
        04-fix-switch-tests.patch
        05-retry-loop.patch
        06-svnadmin-binary-mode.patch
        07-fix-path-tests.patch
        08-ruby-test-fix.patch
        09-wincrypt.patch
        10-perl-vendors.patch
        11-XFAIL-case-sensitive-move-tests.patch
        12-fix-svnlook-test.patch
        13-dso_open.patch
        14-fix_svnauthz_tests.patch
        15-XFAIL_case_sensitive_tree_conflict.patch
        16-fix-swig3.patch
        17-fix-test-link.patch
        18-fix-serf-config.patch
        19-remove-contrib-from-configure.patch
        subversion-1.9.1-msys2.patch
        remove-checking-symlink.patch
        90-use-copy-instead-symlink.patch)
sha256sums=('6ba8e218f9f97a83a799e58a3c6da1221d034b18d9d8cbbcb6ec52ab11722102'
            'SKIP'
            'b09dd041aba0078c8d50df130ef2f96c3ef8486279a620532fef4fe48ef9961e'
            '66d8028dbee086502e453d7435aa15f7e141dbc8808e650e883e1d90a0a022b1'
            '6dc4dda8291087abb94ac0e256833b2ecae452a597fc9222e7443ea0e5694ff7'
            '96a786a619cc7d45d2c3dcc6851579f7e3cb65fb8d254bb817227439465041a0'
            'ea93b62a7f71437c7066aa489044fc311457ea330b9735347a4609858e12c2ad'
            '2504c157f51cfb6453205da3ef2705ad6f36b63169fde3efaf438b7dc4ecc306'
            '1ee9719475a89e3447fade44e704a32e63c8a2916b38b68984f1cb6a4dae158a'
            '6445d7f0758e3ddbd91e35612c545c1f4f5cecbd1c8e134b6b8ea66e8a39e6f0'
            'edad1ae1db35522af533efa8346885a16e9bbdf43ad8196827e8411ca84d7825'
            'a93ad1ccbcc2e68ef3a50fed9b529107aa434acb6618ddb28f4fde934bfcb3b8'
            '5ef14d187071c60de834d87dbea18d3fd022fd11a648dc5315bc7b9fa7067141'
            'eca0edb5d2afbbe8a7d556d8afb79000e653f1e4662b62157368d00f58dd9fff'
            '4069a84189db7ed0d08c5fda927308d494ac9377e111d70debdbb6ad889af70b'
            '21038203297180e74bd75878aae6ce5c1c299ca56d71075bb7029e4dbf083fa2'
            'cad2c464dc3ae6753b1ce2f330d984c97679a52ac1baa411981cfa8c7ebcf312'
            '7f9e60b99a2ec8424f702b5dc07e5b1d37c8eb7d8bd134cc79a7024cb35292d6'
            '8682d9f7da2ba8607ed15daf3beec92e5dacda3a6e4c07baaef8991264eb44cd'
            '0f78c288ec619567f175f473e6a42cc24c185d763b7da5c2c3579d8bab366aba'
            'f61c9b3333d90790a4c6763d81ee05ee5a2029e562c900e92f7526393460181c'
            'a1072514d1be6e2026ae1e2fa24d3bfd97dfbda13b4bd4d89aae0e871b2aae32'
            'c89de3d5272b0196440d6fdf21c14170d0c41702c84fbb273ec87c115633d809'
            'd821304ee957ef4adbb0a8dec9cea3fe62ad7163aef132317d28f70ea25162ca'
            'fc545e375d8af4ef6d8afad453453480664b6a2ff74915f94b8a7da7de14155b')

case "$(uname -m)" in
i686)
# py3c was not built in 32-bit MSYS2; In fact, all 32-bit development of MSYS2 stopped.
# Just skip the Python bindings; Git for Windows does not need them.
makedepends=('perl' 'swig' 'ruby' 'liblz4-devel' 'libsqlite-devel' 'libserf-devel' 'libsasl-devel' 'gmp-devel')
PYTHON_SITELIB=
;;
*)
PYTHON_SITELIB=$(/usr/bin/python -c 'from distutils.sysconfig import * ; print(get_python_lib(0,0));')
;;
esac

prepare() {
  cd ${startdir}/
  [[ -d ${srcdir}/${pkgname}-${pkgver} ]] && rm -rf -d ${srcdir}/${pkgname}-${pkgver}
  tar -xjvf ${SRCDEST}/${pkgname}-${pkgver}.tar.bz2 -C ${srcdir} || true

  cd ${srcdir}/${pkgname}-${pkgver}

  sed -i 's|except IOError,|except IOError as|' build/getversion.py

  patch -p1 -i ${srcdir}/01-make-build-directories.patch
  patch -p1 -i ${srcdir}/02-fix-linking.patch
  patch -p1 -i ${srcdir}/03-fix-linking-perl.patch
  patch -p1 -i ${srcdir}/04-fix-switch-tests.patch
  patch -p1 -i ${srcdir}/05-retry-loop.patch
  patch -p1 -i ${srcdir}/06-svnadmin-binary-mode.patch
  patch -p1 -i ${srcdir}/07-fix-path-tests.patch
  patch -p1 -i ${srcdir}/08-ruby-test-fix.patch
  patch -p1 -i ${srcdir}/09-wincrypt.patch
  patch -p1 -i ${srcdir}/10-perl-vendors.patch
  patch -p1 -i ${srcdir}/11-XFAIL-case-sensitive-move-tests.patch
  patch -p1 -i ${srcdir}/12-fix-svnlook-test.patch
  patch -p1 -i ${srcdir}/13-dso_open.patch
  patch -p1 -i ${srcdir}/14-fix_svnauthz_tests.patch
  patch -p1 -i ${srcdir}/15-XFAIL_case_sensitive_tree_conflict.patch
  patch -p1 -i ${srcdir}/16-fix-swig3.patch
  patch -p1 -i ${srcdir}/17-fix-test-link.patch
  patch -p1 -i ${srcdir}/18-fix-serf-config.patch
  patch -p1 -i ${srcdir}/19-remove-contrib-from-configure.patch
  patch -p1 -i ${srcdir}/subversion-1.9.1-msys2.patch
  patch -p1 -i ${srcdir}/remove-checking-symlink.patch
  patch -p1 -i ${srcdir}/90-use-copy-instead-symlink.patch

  PYTHON=/usr/bin/python2 ./autogen.sh
}

build() {
  export lt_cv_deplibs_check_method='pass_all'
  cd ${pkgname}-${pkgver}
  export PYTHON=/usr/bin/python
  ./configure --prefix=/usr \
      --build=${CHOST} \
      --with-apr=/usr \
      --with-apr-util=/usr \
      --with-zlib=/usr \
      --with-serf=/usr \
      --with-sqlite=/usr \
      --without-apxs \
      --without-gnome-keyring \
      --with-utf8proc=internal \
      --enable-shared --disable-static \
      --with-ruby-sitedir=/usr/lib/ruby/vendor_ruby \
      --disable-mod-activation \
      --enable-disallowing-of-undefined-references \
      --enable-local-library-preloading

  test -n "$PYTHON_SITELIB" || {
    sed -i 's/\(swig:.*\) [^ ]*-py/\1/' Makefile
    sed -i 's/\(swig:.*\) [^ ]*-py/\1/' build-outputs.mk
  }

  make clean-swig
  make autogen-swig
  make all

  plain "Compiling swig bindings: ruby"
  make swig-rb

  plain "Compiling swig bindings: perl"
  make swig-pl

  test -z "$PYTHON_SITELIB" || {
  plain "Compiling swig bindings: python"
  make swig-py swig_pydir=${PYTHON_SITELIB}/libsvn swig_pydir_extra=${PYTHON_SITELIB}/svn
  }
}

#check() {
#   cd ${pkgname}-${pkgver}
#   export LANG=C LC_ALL=C
#   make check check-swig-pl check-swig-py check-swig-rb CLEANUP=yes # check-javahl
#}

package() {
  cd ${pkgname}-${pkgver}

  make -j1 DESTDIR="${pkgdir}" INSTALLDIRS=vendor \
    swig_pydir=${PYTHON_SITELIB}/libsvn \
    swig_pydir_extra=${PYTHON_SITELIB}/svn \
    install install-tools $(test -z "$PYTHON_SITELIB" || echo install-swig-py) install-swig-pl install-swig-rb

  install -dm755 "${pkgdir}"/usr/share/subversion
  cp -a tools/hook-scripts "${pkgdir}"/usr/share/subversion/
  rm "${pkgdir}"/usr/share/subversion/hook-scripts/*.in

  ## svnclean
  install -Dm 755 ${srcdir}/svnclean "${pkgdir}"/usr/bin/svnclean

  install -Dm 644 tools/client-side/bash_completion \
     "${pkgdir}"/usr/share/bash-completion/completions/subversion
  for i in svn svnadmin svndumpfilter svnlook svnsync svnversion; do
      ln -sf subversion "${pkgdir}"/usr/share/bash-completion/completions/${i}
  done

  # Remove illegal files
  find ${pkgdir}/usr -type f -name "*::*" | xargs rm -f

  # fix permissons
  find ${pkgdir}/usr -type f \( -name *.dll -o -name *.exe \) | xargs chmod 0755
}
